﻿package code {
	import flash.display.Sprite;
	// class is main gameobject.  Basically what everything in the game is made out of
	public class GameObject extends Sprite {
		public var ax:Number;  // the acceleration in x direction
		public var ay:Number;  // acceleration in the y direction
		public var vx:Number;  // velocity in x direction
		public var vy:Number;  // velocity in y direction
		public var theta:Number;
		public var shapeType:String;  // type of shape the object is 
		public var shape:BaseShape;   // the stored shape
		public var isDeformed:Boolean;
		
		public function GameObject(sType:String, radius:Number, sides:Number, color:uint, thickness:Number, hollow:Boolean, angle:Number = 0, lineHeight:int = 0, thirdVertex:Vertex = null) {
			ax = 0;
			ay = 0;
			vx = 0;
			vy = 0;
			theta = 0;
			shapeType = sType;
			isDeformed = false;
			init(shapeType, radius, sides, color, thickness, hollow, angle, lineHeight, thirdVertex);
		}
		
		// initializes the game object based on the shape
		private function init(shapeType:String, radius:Number, sides:Number, color:uint, thickness:Number, hollow:Boolean, angle:Number, lineHeight:int = 0, thirdVertex:Vertex = null){
			if(shapeType == "Polygon"){  
				shape = new Polygon(radius, sides, color, thickness, hollow, angle);
			}
			else if(shapeType == "Rectangle"){
				shape = new Rect(radius, sides, color, thickness, hollow, angle, lineHeight); // rectangles need a line height
			}
			else if(shapeType == "Line"){
				shape = new Line(new Vertex(0, 0), new Vertex(0, 215), thickness, color);
			}
			
			//trace(shape);
			addChild(shape);  // add shapes as a child of gameobject
		}
		
		// method creates array of lines based off of the shape.  Must be created after x and y are assigned
		public function createLinesArray():Array{
			var verticesToPush = new Array();  // array of vertices to check.  Kept seperate from main vertices of shape
			var linesToPush = new Array();	// the lines to push in the main game class. A debugging feature to see how it would work
			shape.linesArray = new Array();
			if(shape.vertices.length > 0){  // to prevent a glitch where the we were getting null vertices
				for each(var v:Vertex in shape.vertices){
					verticesToPush.push(new Vertex(v.x + x, v.y + y)); // pushes all the vertices
				}
				verticesToPush.push(new Vertex(shape.vertices[0].x + x, shape.vertices[0].y + y));  // pushes the first vertex so it will loop back
				for(var i:int = 0; i < verticesToPush.length - 1; i++){	// goes through the vertices and creates line out of them
					var tempLine:Line = new Line(verticesToPush[i], verticesToPush[i + 1], 3, 0x000000);  // creates the temp line to push
					shape.linesArray.push(tempLine);  // stores it in shape
					linesToPush.push(tempLine);  // puts them into the output debug array
				}
			}
			return linesToPush;
		}
		
	}
	
}
